﻿using System.IO;

namespace SdmxMl.Common
{
    /// <summary> Basic methods for CSV persistence </summary>
    public class CsvExportBase
    {
        #region Internal Members

        protected char delimiter;

        #endregion

        #region C'tor

        public CsvExportBase(char sep)
        {
            delimiter = sep;
        }

        #endregion

        #region Export helpers

        protected void WriteItem(StreamWriter sw, string s, bool quoteall)
        {
            if (s == null)
                return;
            if (quoteall || s.IndexOfAny("\",\x0A\x0D".ToCharArray()) > -1)
                sw.Write("\"" + s.Replace("\"", "\"\"") + "\"");
            else
                sw.Write(s);
        }

        protected void AddCell(StreamWriter sw, int col, int colCount, string val, bool quoteall)
        {
            WriteItem(sw, val, quoteall);
            if (col < colCount)
                sw.Write(delimiter);
            else
                sw.Write('\n');
        }


        #endregion

    }

}
